package portal.conf;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Vector;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import portal.common.MyStringUtils;
import portal.conf.entity.BaseConf;
import portal.conf.entity.GPUEntity;
import portal.conf.entity.HostEntity;
import portal.conf.entity.HostGroupEntity;
@Component
@Scope("singleton")
public class ElimGPU extends AbstractConf {
	public ElimGPU(){
		if(IsDebug)
			setFileName("//app//unischeduler//sbin//linux-x86_64//elim.gpu");
	}
	@Override
	@Value("${conf.elim.gpu}")  
	public void setFileName(String fname) {  
		super.setFileName(fname);
	} 
	@Override
	protected boolean isConfStart(String line) {
		if(line.contains("DB_Res[")&&line.contains("DB_Name[")&&line.contains("DB_Info["))
			return true;
		return false;
	}
	
	@Override
	protected boolean isConfEnd(String line) {
		return isConfStart(line);
	}
	@Override
	protected  BaseConf analysiConf(String tempString,int line,Class<?> t,BaseConf qe){
		
		try {
			if(!isConfStart(tempString)){
				//clearConfList();
				return qe;
			}
			qe=(BaseConf) t.newInstance();
			qe.setLineStart(line);
			qe.setLineEnd(line);
			Method method =Class.forName(t.getName()).getMethod("setValue",String.class);	
			method.invoke(qe,MyStringUtils.trimAll(tempString));
			addToConfList(qe);
			
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return qe;
	}
	//@Override
	public boolean insertConf(String res,String name, String info){
		return insertConf(new GPUEntity(res,name,info));
	}
	public boolean insertConf(BaseConf bConf){
		if(isEntityExist(bConf))return false;
		if(confList==null ||confList.isEmpty())return false;
		BaseConf b=(BaseConf)confList.get(confList.size()-1);//.getStart
		((GPUEntity)bConf).checkIndex();;
		bConf.setLineStart(b.getLineStart());
		bConf.setLineEnd(bConf.getLineStart());
		updateFile(bConf, fileName, 1);
		selectAllConf(new File(fileName),GPUEntity.class );//selectAllConf();
		//System.out.println(getLastModified());
		return true;
	}
	public List<BaseConf> selectAllConf(){
		return super.selectAllConf(fileName, GPUEntity.class);
	}
	public static void main(String[] args){
		ElimGPU queueConf=new ElimGPU();
		List<BaseConf> list=queueConf.selectAllConf();
		queueConf.insertConf("TeslaM20702","Tesla M20702","NVIDIA GPU Tesla M2072");
		for(int index=0;index<list.size();index++){
			System.out.println(list.get(index));
		}
	}
}
